package com.xt.yncu.soap;

/**
 *  <br>
 * <br>
 * 
 * <p>
 * <a href=".java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @create-date:2011-11-2
 */
import java.io.Serializable;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xt.base.wireless.server.MobileActionUtil;


public class SOAPInterface implements Serializable{
	private static final long serialVersionUID = -8083385063260545321L;
	
	private final static Logger logger = Logger.getLogger(MobileServiceService.class);

	private String uri;
	private String uriMail;
	private String uriSms;
	private Map<String,String> webserviceUrlMap;
	private Map<String,MobileService> mobileServiceMap;
	
	private String method;
	private String username;
	private String password;
	private boolean cert=false;
	
	private boolean first = true;
	
	private String urn ;
	private String webServiceClientName;
	private String serverType;
	
	private URL urlObj;
	
	private String returnMsg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><mobile><exception>WebService服务端长时间没有响应</exception></mobile>";
	
	public void init(){
		URL baseUrl = SOAPInterface.class.getResource(".");
		Set<String> set = webserviceUrlMap.keySet();
		mobileServiceMap = new HashMap<String,MobileService>();
		MobileService ms = null;
		for(String s:set){
			try {
				if ("K".equals(s) || "mailL".equals(s) || "mailT".equals(s)) {
					urlObj = new URL(baseUrl, uriMail + webserviceUrlMap.get(s));
					ms = new MobileServiceService(urlObj,
							new QName(urn, webServiceClientName),serverType).getPort(
							new QName(urn, serverType), MobileService.class);
					mobileServiceMap.put(s, ms);
				}else if ("smsT".equals(s)) {
					urlObj = new URL(baseUrl, uriSms + webserviceUrlMap.get(s));
					ms = new MobileServiceService(urlObj,
							new QName(urn, webServiceClientName),serverType).getPort(
							new QName(urn, serverType), MobileService.class);
					mobileServiceMap.put(s, ms);
				} else {
					urlObj = new URL(baseUrl, uri + webserviceUrlMap.get(s));
					ms = new MobileServiceService(urlObj,
							new QName(urn, webServiceClientName),serverType).getPort(
							new QName(urn, serverType), MobileService.class);
					mobileServiceMap.put(s, ms);
				}
			} catch (Exception e) {
				logger.error("Failed to create URL for the wsdl Location: '"
						+uri+"', retrying as a local file",e);
			}
		}
		
		
		
	}
	
	
	public String send(Map<String,Object> map){
		if(first){
			init();
			first = false;
		}
		
		String xml = MobileActionUtil.parseResultMap2Xml(map);
		logger.info("---- SOAP访问 start --- \n传入的xml="+xml);
		logger.info("---- SOAP访问 正在等待响应 --- ");
		String tmp = new String("<![CDATA["+xml+"]]>");
		String funcid ;//= map.get("mainfuncid")==null?null:String.valueOf(String.valueOf(map.get("mainfuncid")).charAt(0));
		//if(StringUtils.isEmpty(funcid)){
			funcid = String.valueOf(String.valueOf(map.get("funcid")).charAt(0));
		//}else{//在取附件时，需要指定附件属于具体的模块（不同的webservice路径）下。
			if(map.get("mainfuncid")!=null){
				if("B002".equals(map.get("mainfuncid"))){
					map.put("mainfuncid","Z");
				}else{
					map.put("mainfuncid",String.valueOf(String.valueOf(
							map.get("mainfuncid")).charAt(0)));
				}
			}
		//}
		//获取邮件附件时，需要调整webservice名称索引的key
		/*if(map.get("mainfuncid")!=null
				&&map.get("mainfuncid").toString().indexOf("K")>=0//邮件附件
				&&"L".equals(String.valueOf(String.valueOf(map.get("funcid")).charAt(0)))){
			funcid="mailL";
		}else if(map.get("mainfuncid")!=null
				&&map.get("mainfuncid").toString().indexOf("K")>=0//邮件树
				&&"T".equals(String.valueOf(String.valueOf(map.get("funcid")).charAt(0)))){
			funcid="mailT";
		}else if(map.get("mainfuncid")!=null
				&&map.get("mainfuncid").toString().indexOf("L")>=0//短信树
				&&"T".equals(String.valueOf(String.valueOf(map.get("funcid")).charAt(0)))){
			funcid="smsT";
		}*/
		System.out.println(funcid);
			
		this.returnMsg = mobileServiceMap.get(funcid).rtInterface(tmp);
		
		//if(this.returnMsg==null || this.returnMsg.length()<1000){
			logger.info("---- SOAP访问 响应结果为 :\n"+this.returnMsg);
		//}
		logger.info("---- SOAP访问 end --- ");
		return returnMsg;
	}
	


	public String getMethod() {
		return method;
	}


	public void setMethod(String method) {
		this.method = method;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isCert() {
		return cert;
	}

	public void setCert(boolean cert) {
		this.cert = cert;
	}


	public String getUrn() {
		return urn;
	}


	public void setUrn(String urn) {
		this.urn = urn;
	}


	public String getWebServiceClientName() {
		return webServiceClientName;
	}


	public void setWebServiceClientName(String webServiceClientName) {
		this.webServiceClientName = webServiceClientName;
	}


	public String getUri() {
		return uri;
	}


	public void setUri(String uri) {
		this.uri = uri;
	}


	public Map<String, String> getWebserviceUrlMap() {
		return webserviceUrlMap;
	}


	public void setWebserviceUrlMap(Map<String, String> webserviceUrlMap) {
		this.webserviceUrlMap = webserviceUrlMap;
	}


	public String getServerType() {
		return serverType;
	}


	public void setServerType(String serverType) {
		this.serverType = serverType;
	}


	public String getUriMail() {
		return uriMail;
	}


	public void setUriMail(String uriMail) {
		this.uriMail = uriMail;
	}


	public String getUriSms() {
		return uriSms;
	}


	public void setUriSms(String uriSms) {
		this.uriSms = uriSms;
	}
}
